<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>连接数据模型</title>
    <script src="../js/react.js"></script>
    <script src="../js/JSXTransformer.js"></script>
    <script src="../js/react-dom.js"></script>
    <script src="../js/jquery-2.1.4.min.js"></script>
    <!-- <script src="../js/browser.min.js"></script>-->
    <script src="http://cdnjs.cloudflare.com/ajax/libs/showdown/0.3.1/showdown.min.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/jsx">

    var converter = new Showdown.converter();
    var Comment = React.createClass({
        render: function () {
            var rawMarkup = converter.makeHtml(this.props.children.toString());
            return (
                    <div>
                        <h2>{this.props.author}</h2>
                        <span dangerouslySetInnerHTML={{__html:rawMarkup}}></span>
                    </div>
            );
        }
    });

    var CommentList = React.createClass(
            {
                render: function () {
                    var commentNodes = this.props.data.map(function (comment) {
                        return (
                                <Comment author={comment.author}>
                                    {comment.text}
                                </Comment>
                        );
                    });
                    return (
                            <div>
                                {commentNodes}
                            </div>
                    );
                }
            });

    var CommentForm = React.createClass(
            {
                render: function () {
                    return (
                            <div>
                                Hello,world!I am a CommentForm..
                            </div>
                    );
                }
            });

    var CommentBox = React.createClass({

        getInitialState: function () {
            return {data: []};
        },
        componentDidMount: function () {
            $.ajax({
                url: this.props.url,
                dataType: 'json',
                success: function (data) {
                    this.setState({data: data});
                }.bind(this),
                error: function (xhr, status, err) {
                    console.error(this.props.url, status, err.toString());
                }.bind(this)
            });
        },
        render: function () {
            return (
                    <div className="commentBox">
                        <h1>Comments</h1>
                        <CommentList data={this.state.data}/>
                        <CommentForm />
                    </div>
            );
        }
    });

    ReactDOM.render(
            <CommentBox url="comments.json" pollInterval={2000}/>,
            document.getElementById('example')
    );


</script>
</body>
</html>